﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.Recycling.Object;
using Imaginecup.recycling.Object.Item;
using Imaginecup.recycling.Object.Menu;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input;
using Imaginecup.util;
using Imaginecup.Recycling.GameMan;
using Imaginecup.gameState.recycling.GameMan;
using Imaginecup.Engine;

// 각종 아이템을 관리하는 클래스
namespace Imaginecup.recycling.GameMan
{
    public class ItemManager //: IGameObject
    {
        private const int InventorySize = 8;

        private Queue<MapInfo> InventoryList = new Queue<MapInfo>(InventorySize);
        private MenuComponent Inventory = new Menu();
        private int m_CurrentListItemIndex = 0;

        public ItemManager()
        {
            Initialize();
        }

        public void Initialize()
        {
            for (int i = 0; i < InventorySize; i++)
            {
                MenuComponent menu = new Menu(new Vector2(0 + i * 100, 0), new Rectangle(0 + i * 100, 0, 100, 100));
                Inventory.Add(menu);
            }
        }

        public void AddItem(MapInfo mapInfo)
        {
            // 인벤토리가 가득 찼으면 리턴(또는 캐릭터 Die)
            if (m_CurrentListItemIndex == InventorySize)
            {
                return;
                //ItemListNumber = 0;
            }
            MenuComponent item = new MenuItem(
                ResourceManager.GetInstance().GetContent(mapInfo.m_FileName), 
                new Vector2(0 + m_CurrentListItemIndex * 100, 0), mapInfo.m_DrawRect, mapInfo.m_State);
            Inventory.GetChild(m_CurrentListItemIndex).Add(item);
            InventoryList.Enqueue(mapInfo);
            m_CurrentListItemIndex++;
        }

        public bool IsGestureOfItem(MapState gesture)
        {
            // 아이템이 있다면 아이템을 처리하고 리스트 쉬프트
            if(Inventory.GetChild(0).GetChild(0).GetItemID() == gesture)
            {
                // 인벤토리에 있는 아이템 리스트에서 첫번재 아이템을 제거하고 쉬프트
                ShiftItemList();
                return true;
            }
            return false;
        }
        // 1. 오른쪽, 2. 위쪽, 3. 왼쪽, 4. 아래쪽
        public bool IsGestureOfItem(int nGesture)
        {
            return IsGestureOfItem(GetGestureByState(nGesture));
        }

        public MapState GetGestureByState(int nGesture)
        {
            MapState gestureState = MapState.NONE;
            switch (nGesture)
            {
                case 1:
                    gestureState = MapState.CAN;
                    break;
                case 2:
                    gestureState = MapState.GLASS;
                    break;
                case 3:
                    gestureState = MapState.PLASTIC;
                    break;
                case 4:
                    gestureState = MapState.PAPER;
                    break;
            }
            return gestureState;
        }

        private void ShiftItemList()
        {
            int count = InventoryList.Count;
            for(int i=0; i<count-1; i++)
            {
                MapInfo topMapInfo = InventoryList.ElementAt(i + 1);
                Inventory.GetChild(i).Clear();  // 현재의 인벤토리를 비움
                MenuComponent item = new MenuItem(
                    ResourceManager.GetInstance().GetContent(topMapInfo.m_FileName), 
                    new Vector2(0 + i * 100, 0),
                    topMapInfo.m_DrawRect, topMapInfo.m_State);

               Inventory.GetChild(i).Add(item);             // 새로운 아이템을 추가
            }
            Inventory.GetChild(count-1).Clear();
            m_CurrentListItemIndex--;

            InventoryList.Dequeue();
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Inventory.Draw(spriteBatch, gameTime);
        }

        public void Update(GameTime gameTime)
        {
            Inventory.Update(gameTime);
#if WINDOWS_PHONE
            // 아이템이 하나도 없으면 리턴 FALSE
            if (!Inventory.GetChild(0).IsItem())
            {
                GestureInputHandler.GetInputHandler().Gesture = 0;
                return;
            }

            if (IsGestureOfItem(GestureInputHandler.GetInputHandler().Gesture))
            {
                GestureInputHandler.GetInputHandler().Gesture = 0;
            }
#endif
#if WINDOWS 
            IKeyboardInputHandler keyboard = KeyboardInputHandler.GetInputHandler();
            MapState inputState = MapState.NONE;
            if(keyboard.IsOneKeyDown(Keys.Right))
            {
                inputState = MapState.CAN;
            }
            else if (keyboard.IsOneKeyDown(Keys.Up))
            {
                inputState = MapState.GLASS;
            }
            else if (keyboard.IsOneKeyDown(Keys.Left))
            {
                inputState = MapState.PLASTIC;
            }
            else if (keyboard.IsOneKeyDown(Keys.Down))
            {
                inputState = MapState.PAPER;
            }
            if (Inventory.GetChild(0).IsItem() && inputState != 0)
                IsGestureOfItem(inputState);

#endif
#if XBOX
            IGamePadHandler gamePad = GamePadHandler.GetInputHandler();
            int key = 0;
            if(gamePad.IsOneDown(Buttons.B))
                key = 1;
            else if (gamePad.IsOneDown(Buttons.Y))
                key = 2;
            else if (gamePad.IsOneDown(Buttons.X))
                key = 3;
            else if (gamePad.IsOneDown(Buttons.A))
                key = 4;
            if (Inventory.GetChild(0).IsItem() && key != 0)
                IsGestureOfItem(key);
            
#endif
        }
    }
}
